package cn.com.egova.demoapplication.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewPropertyAnimator;

import com.nineoldandroids.view.ViewHelper;

/**
 * Created by yuanchao on 2017/3/7.
 */

public class RoundView extends View {
    public static final String TAG = "RoundView";
    private int screenWidh;
    private int width = 100;
    private int height = 100;
    private Paint paint;

    public RoundView(Context context) {
        this(context, null);
    }

    public RoundView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.RED);
        paint.setStrokeWidth(2);
        paint.setStyle(Paint.Style.FILL);

        DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
        screenWidh = metrics.widthPixels;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(width, height);
    }


    @Override
    protected void onDraw(Canvas canvas) {
//        float radius = (float) Math.sqrt(Math.pow(width / 2, 2) + Math.pow(height / 2, 2));
        canvas.drawCircle(width / 2, height / 2, width / 2, paint);
    }


    float lastX;
    float lastY;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float rawX = event.getRawX();
        float rawY = event.getRawY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:

                break;
            case MotionEvent.ACTION_MOVE:
                float disX = rawX - lastX;
                float disY = rawY - lastY;

                //为了兼容性和效果尽量用nineoldandroid
                float translationX = ViewHelper.getTranslationX(this) + disX;
                float translationY = ViewHelper.getTranslationY(this) + disY;
                //给view设置平移动画.3.0以下实质还是Tween动画,3.0以上为属性动画.
                ViewHelper.setTranslationX(this, translationX);
                ViewHelper.setTranslationY(this, translationY);


                //使用此方式虽然也能动,但是不流畅,原因暂时不明确.
//                float translationX = this.getTranslationX() + disX;
//                float translationY = this.getTranslationY() + disY;
//                updatePosition(translationX, translationY);

                break;
            case MotionEvent.ACTION_UP:
//                if(lastX<screenWidh/2){
//                    float transX = -ViewHelper.getTranslationX(this);
//                    ViewHelper.setTranslationX(this, transX);
//                    lastX = 0;
//                    return true;
//                }else{
////                    float transX = ViewHelper.getTranslationX(this) + (screenWidh-lastX)-width/2;
//                    float transX = screenWidh-ViewHelper.getTranslationX(this)+width;
//                    ViewHelper.setTranslationX(this, transX);
//                    lastX = screenWidh;
//                    return true;
//                }
        }

        lastX = rawX;
        lastY = rawY;
        return true;
    }


    /**
     * 通过平移动画更新位置的方法.
     *
     * @param translationx
     * @param translationy
     */
    ViewPropertyAnimator viewPropertyAnimator = null;
    private void updatePosition(float translationx, float translationy) {
        if(viewPropertyAnimator==null){
            viewPropertyAnimator = this.animate();
        }
        viewPropertyAnimator.translationX(translationx).translationY(translationy).setDuration(1);
    }

}
